<?php
/**
 * Created by PhpStorm.
 * User: peter
 * Date: 2018/9/10
 * Time: 下午4:37
 */

namespace app\activity\controller;


use extend\Functions;
use extend\STATUS_CODE;
use think\Request;

class Member extends Auth
{
    /**
     * @purpose 获取用户信息
     * @return array
     */
    public function info()
    {
        return Functions::result(
            STATUS_CODE::SUCCESS,
            [
                'uuid'     => self::$uuid,
                'nickname' => self::$user_info->nickname,
                'avatar'   => self::$user_info->avatar,
                'balance'  => self::$user_info->balance - self::$user_info->frozen,
                'sex'      => self::$user_info->sex,
                'mobile'   => self::$user_info->mobile ? Functions::hide_mobile(self::$user_info->mobile) : '',
                'username' => self::$user_info->username,
                'password' => self::$user_info->password,
                'develop' => self::$user_info->is_develop,
            ]);
    }

    /**
     * @purpose 设置昵称
     * @param Request $request
     * @return array
     */
    public function setNickname(Request $request)
    {
        $nickname = $request->post('nickname', '');
        return \app\activity\logic\Member::setNickname($nickname, self::$uid);
    }

    /**
     * @purpose 设置用户名
     * @param Request $request
     * @return array
     */
    public function setUsername(Request $request)
    {
        $username = $request->post('username', '');
        return \app\activity\logic\Member::setUsername($username, self::$uid);
    }

    /**
     * @purpose 设置密码
     * @param Request $request
     * @return array
     */
    public function setPassword(Request $request)
    {
        $password = $request->post('password', '');
        return \app\activity\logic\Member::setPassword($password, self::$uid);
    }

    /**
     * @purpose 编辑密码
     * @param Request $request
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function editPassword(Request $request)
    {
        $old_password = $request->post('old_password', '');
        $new_password = $request->post('new_password', '');
        return \app\activity\logic\Member::editPassword($old_password, $new_password, self::$uid);
    }

    /**
     * @purpose 绑定手机号码
     * @param Request $request
     * @return array
     */
    public function bindMobile(Request $request)
    {
        $mobile = $request->post('mobile', '');
        $code   = $request->post('code', '');
        return \app\activity\logic\Member::bindMobile($mobile, $code, self::$uid);
    }

    /**
     * @purpose 登录后的用户获取验证码
     * @return array
     */
    public function getCode()
    {
        return \extend\SMS::smsCode(self::$user_info['mobile']);
    }

    /**
     * @purpose 更换绑定手机号
     * @param Request $request
     * @return array
     */
    public function changeMobile(Request $request)
    {
        $old_mobile = $request->post('old_mobile', '');
        $new_mobile = $request->post('new_mobile', '');
        $old_code   = $request->post('old_code', '');
        $new_code   = $request->post('new_code', '');
        return \app\activity\logic\Member::changeMobile($old_mobile, $old_code, $new_mobile, $new_code, self::$uid);
    }
}